package cn.hellochaos.generator.service.impl;

import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.CaipuSummary;
import cn.hellochaos.generator.mapper.CaipuSummaryMapper;
import cn.hellochaos.generator.service.CaipuSummaryService;
import cn.hellochaos.generator.utils.CheckObjectIsNullUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import cn.hellochaos.generator.exception.bizException.BizException;

import java.util.List;

/**
* <p>
* 简要介绍 服务实现类
* </p>
*
* @author test
* @since 2021-01-23
*/
@Slf4j
@Service
public class CaipuSummaryServiceImpl extends ServiceImpl<CaipuSummaryMapper, CaipuSummary> implements CaipuSummaryService {

    @Autowired
    CaipuSummaryMapper caipuSummaryMapper;

    @Override
    public CaipuSummary getCaipuSummaryByCookbookId(int cookbookId) {

        QueryWrapper<CaipuSummary> queryWrapper = new QueryWrapper<CaipuSummary>();
        queryWrapper.eq("id_cookbook",cookbookId);

        List<CaipuSummary> caipuLists = caipuSummaryMapper.selectList(queryWrapper);
        if (caipuLists.size()==0){
            return new CaipuSummary();
        }
        return caipuLists.get(0);
    }

    @Override
    public Page<CaipuSummary> listCaipuSummarysByPage(int page, int pageSize, String factor) {
        log.info("正在执行分页查询caipuSummary: page = {} pageSize = {} factor = {}",page,pageSize,factor);
        QueryWrapper<CaipuSummary> queryWrapper =  new QueryWrapper<CaipuSummary>().like("", factor);
        //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法
        Page<CaipuSummary> result = super.page(new Page<>(page, pageSize));
        log.info("分页查询caipuSummary完毕: 结果数 = {} ",result.getRecords().size());
        return result;
    }

    @Override
    public CaipuSummary getCaipuSummaryById(int id) {
        log.info("正在查询caipuSummary中id为{}的数据",id);
        CaipuSummary caipuSummary = super.getById(id);
        log.info("查询id为{}的caipuSummary{}",id,(null == caipuSummary?"无结果":"成功"));
        return caipuSummary;
    }

    @Override
    public int insertCaipuSummary(CaipuSummary caipuSummary) {
        log.info("正在插入caipuSummary");
        if (super.save(caipuSummary)) {
            log.info("插入caipuSummary成功,id为{}",caipuSummary.getCaipuSummaryId());
            return caipuSummary.getCaipuSummaryId();
        } else {
            log.error("插入caipuSummary失败");
            throw new BizException("添加失败");
        }
    }

    @Override
    public int deleteCaipuSummaryById(int id) {
        log.info("正在删除id为{}的caipuSummary",id);
        if (super.removeById(id)) {
            log.info("删除id为{}的caipuSummary成功",id);
            return id;
        } else {
            log.error("删除id为{}的caipuSummary失败",id);
            throw new BizException("删除失败[id=" + id + "]");
        }
    }

    @Override
    public int updateCaipuSummary(CaipuSummary caipuSummary) {
        log.info("正在更新id为{}的caipuSummary",caipuSummary.getCaipuSummaryId());
        if (super.updateById(caipuSummary)) {
            log.info("更新d为{}的caipuSummary成功",caipuSummary.getCaipuSummaryId());
            return caipuSummary.getCaipuSummaryId();
        } else {
            log.error("更新id为{}的caipuSummary失败",caipuSummary.getCaipuSummaryId());
            throw new BizException("更新失败[id=" + caipuSummary.getCaipuSummaryId() + "]");
        }
    }

}
